package com.example.video.admin_server.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.video.admin_server.entity.SysRole;
import com.example.video.admin_server.entity.vo.SysRoleDetailsVo;
import com.example.video.admin_server.service.ISysRoleService;
import com.example.video.common.Constant;
import com.example.video.common.util.ResponseWrap;
import com.example.video.common.util.ResponseWrapUtil;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 系统角色表 前端控制器
 * </p>
 *
 * @author yj
 */
@RestController
@RequestMapping("/admin/sys-role")
public class SysRoleController {

    @Autowired
    private ISysRoleService iSysRoleService;

    @PostMapping ("/add")
    @ApiOperation(value = "新增角色")
    public ResponseWrap<SysRole> add(@RequestBody SysRoleDetailsVo sysRoleDto){
        boolean save = iSysRoleService.saveRole(sysRoleDto);
        return ResponseWrapUtil.success();
    }

    @DeleteMapping ("/delRole")
    @ApiOperation(value = "删除角色")
    public ResponseWrap<SysRole> delRole(Integer id){
        boolean del = iSysRoleService.delRole(id);
        return ResponseWrapUtil.success();
    }

    @PostMapping ("/update")
    @ApiOperation(value = "修改角色")
    public ResponseWrap<SysRole> update(@RequestBody SysRoleDetailsVo sysRoleDto){
        boolean update = iSysRoleService.updateRole(sysRoleDto);
        return ResponseWrapUtil.success();
    }

    @GetMapping("/getList")
    @ApiOperation(value = "条件查询角色列表")
    public ResponseWrap<List<SysRole>> getList(SysRole sysRole){
        List<SysRole> list = iSysRoleService.list(new QueryWrapper(sysRole.setStatus(Constant.Status.ENABLE.value)));
        return ResponseWrapUtil.success(list);
    }

    @GetMapping("/getDetails")
    @ApiOperation(value = "获取角色详情")
    public ResponseWrap<SysRoleDetailsVo> getDetails(Integer id){
        SysRoleDetailsVo sysRoleDetailsVo = iSysRoleService.getDetails(id);
        return ResponseWrapUtil.success(sysRoleDetailsVo);
    }

    @GetMapping("/getPaginationList")
    @ApiOperation(value = "条件分页查询角色列表")
    public ResponseWrap<Page<SysRole>> getPaginationList(Integer pageSize, Integer pageIndex, SysRole sysRole){
        Page<SysRole> page = iSysRoleService.page(new Page<SysRole>(pageIndex, pageSize));
        return ResponseWrapUtil.success(page);
    }


}

